<?php

class Auth extends CI_Controller {

    public function login(){
        $segm = $this->uri->segment(3);
        $data = array('msg' => '');
        if ($segm == 'ready') {
            $data['msg'] = 'Boli ste úspešne zaregistrovaný, teraz sa môžete prihlásiť';
        } else if ($segm=='err') {
            $data['msg'] = 'Neplatná kombinácia mena a hesla';
        }
        
        if ($this->lib_session->isLogged()) {
            return redirect(base_url() . '/tasks/', 'refresh');
        }
        
        $this->load->view('templates/header.php');
        $this->load->view('pages/auth/login.php', $data);
        $this->load->view('templates/footer.php');
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $this->loginForUsername($this->input->post('email'), $this->input->post('password'));
        }
    }

    private function loginForUsername($username, $pass){
           $dbObj = $this->users_model->getUserByUsername($username);
           $hpass=md5($pass);
           
      
           if (!empty($dbObj) && $hpass == $dbObj->password) {
               $this->lib_session->setSession($dbObj);

               $user_id = $this->session->userdata('id');
               $username = $this->session->userdata('firstname').' '.$this->session->userdata('lastname');
               $this->log_model->addLog($user_id, $username . " sa prihlasil.");

                return redirect(base_url() . '/tasks/', 'refresh');
           } else {
              
               return redirect(base_url() . '/auth/login/err', 'refresh');
           }
    }

    public function register(){
        
        $this->load->helper('form');
        $this->load->library('form_validation');

         $segm = $this->uri->segment(3);
        $data = array('msg' => '');
        if ($segm == 'duplicate') {
            $data['msg'] = 'Zadaný email už existuje!';
        }
        
        $this->form_validation->set_rules('email', 'email', 'required|valid_email');
        $this->form_validation->set_rules('meno', 'meno', 'required');
        $this->form_validation->set_rules('priezvisko', 'priezvisko', 'required');
        $this->form_validation->set_rules('password', 'heslo', 'required');
        $this->form_validation->set_rules('year', 'rocnik', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('pages/auth/registration', $data);
            $this->load->view('templates/footer');
        }
        else
        {
            $this->createUser(
                 $this->input->post('email'),
                 md5($this->input->post('password')),
                 $this->input->post('meno'),
                 $this->input->post('priezvisko'),
                 $this->input->post('year')
            );
        }
    }

    private function createUser($username, $pass, $firstname, $lastname, $year) {
        // overenie ci existuje
        $dbObj = $this->users_model->getUserByUsername($username);
        if (!empty($dbObj)) {
            return redirect(base_url() . '/auth/register/duplicate', 'refresh');
        }
        
        // ak nie pridaj
        $this->users_model->registerUser($username, $pass, $firstname, $lastname, $year);
        $logMessage = 'Nova registracia: Meno: ' . $firstname . ' ' . $lastname . ' Rocnik: '. $this->input->post('year') . ' email: '. $username;

        $this->log_model->addLog(NULL,$logMessage);
        
        return redirect(base_url() . '/auth/login/ready', 'refresh');
    }

    public function logout(){
        // Pomocou knižnice Session vymaže SESSION
        $this->load->view('templates/header.php');
        $this->load->view('pages/auth/logout.php');
        $this->load->view('templates/footer.php');

        $user_id = $this->session->userdata('id');
        $username = $this->session->userdata('firstname').' '.$this->session->userdata('lastname');

        $this->lib_session->unsetSession();

        $this->log_model->addLog($user_id, $username . " sa odhlasil.");

         return redirect(base_url() . '/auth/login/', 'refresh');
      //  
    }

}
?>